<html>
<body>
<div>
<a href="#/">首页</a>
<a href="#/product">产品</a>
<a href="#/price">价格</a>
</div>
<!-- 变化的区域内容 -->
<div class="route-content"></div>
</body>
<script type="text/javascript">
const routes = [
{
path: "/",
component: "我是首页"
},
{
path: "/product",
component: "我是产品"
},
{
path: "/price",
component: "我是价格"
}
]
location.hash = "#/"
window.onhashchange = () => {
let hash = location.hash
let route = routes.find((route) => route.path === hash.substring(1))
document.querySelector(".route-content").innerHTML = route.component
}
</script>
</html>